<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\LookofVillageFormat;
use App\Http\Requests\CreateLookofVillageRequest;
use App\Services\LookofVillageService;
use Illuminate\Http\Request;

class LookofVillageController extends Controller
{
    protected $lookofVillageService;

    public function __construct(LookofVillageService $lookofVillageService)
    {
        $this->lookofVillageService = $lookofVillageService;
    }

    public function getList(Request $request)
    {
        $search = [];
        if ($request->input('address_name')) {
            $search['address_name'] = $request->input('address_name');
        }
        $res = $this->lookofVillageService->getList();
        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $page_size = $request->input('page_size', 10);
        $search = [];
        if ($request->input('address_name')) {
            $search['address'] = $request->input('address_name');
        }
        $res = $this->lookofVillageService->getAdminList($page, $page_size, $search);
        return renderSuccessJson($res);
    }

    public function create(CreateLookofVillageRequest $createLookofVillageRequest)
    {
        $format = new LookofVillageFormat($createLookofVillageRequest->all());
        $this->lookofVillageService->create($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $format = new LookofVillageFormat($request->all());
        $this->lookofVillageService->update($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->lookofVillageService->delete($id);
        return renderSuccessJson();
    }

    public function setBanner(Request $request)
    {
        $bannerUrl = $request->input('banner_url');
        if (empty($bannerUrl)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->lookofVillageService->setBanner($bannerUrl);
        return renderSuccessJson();
    }

    public function setXcxBanner(Request $request)
    {
        $bannerUrl = $request->input('banner_url');
        if (empty($bannerUrl)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->lookofVillageService->setXcxBanner($bannerUrl);
        return renderSuccessJson();
    }
}
